/*
 * Copyright 2012-2014 glodon paas All right reserved. This software is the
 * confidential and proprietary information of glodon paas ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with glodon paas.
 */
package com.glodon.paas.framework.ibatis;

import javax.sql.DataSource;

import org.springframework.dao.support.DaoSupport;

import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * @author licx 2013-1-31 下午2:18:37
 */
public class BaseSqlMapClientDaoSupport extends DaoSupport {

	private PaasSqlMapClientTemplate	sqlMapClientTemplate	= null;

	private boolean						externalTemplate		= false;

	public BaseSqlMapClientDaoSupport() {
		sqlMapClientTemplate = (PaasSqlMapClientTemplate) new SqlMapClientInterceptor()
				.proxy(new PaasSqlMapClientTemplate());
	}

	public final void setDataSource(DataSource dataSource) {
		this.sqlMapClientTemplate.setDataSource(dataSource);
	}

	public final DataSource getDataSource() {
		return (this.sqlMapClientTemplate != null ? this.sqlMapClientTemplate.getDataSource() : null);
	}

	public final void setSqlMapClient(SqlMapClient sqlMapClient) {
		this.sqlMapClientTemplate.setSqlMapClient(sqlMapClient);
	}

	public final SqlMapClient getSqlMapClient() {
		return this.sqlMapClientTemplate.getSqlMapClient();
	}

	public final void setSqlMapClientTemplate(PaasSqlMapClientTemplate sqlMapClientTemplate) {
		if (sqlMapClientTemplate == null) {
			throw new IllegalArgumentException("Cannot set sqlMapClientTemplate to null");
		}
		this.sqlMapClientTemplate = sqlMapClientTemplate;
		this.externalTemplate = true;
	}

	public final PaasSqlMapClientTemplate getSqlMapClientTemplate() {
		return sqlMapClientTemplate;
	}

	protected final void checkDaoConfig() {
		if (!this.externalTemplate) {
			this.sqlMapClientTemplate.afterPropertiesSet();
		}
	}
}
